
import { tableModel } from './database.js'
import { createSkeleton } from './skeleton.js'
// input command to console
process.stdin.setEncoding('utf8')
// This function reads only one line on console synchronously. After pressing `enter` key the console will stop listening for data.
function readlineSync () {
  return new Promise((resolve, reject) => {
    process.stdin.resume()
    console.log('please enter scripts...')
    process.stdin.on('data', function (data) {
      process.stdin.pause() // stops after one line reads
      resolve(data)
    })
  })
}

// entry point
async function main () {
  const inputLine = await readlineSync()
  const commands = '--h,-t,-form,-list,-skeleton,-validate,-restful,-router,-store,-view'
  const explation = {
    '-t': { des: '表名,只填-t参数值时将创建对应资源', required: true },
    '-form': { des: '是否生成表单', required: false },
    '-list': { des: '生成列表', required: false },
    '-skeleton': { des: '生成骨架', required: false },
    '-validate': { des: '生成验证', required: false },
    '-restful': { des: '生成restfulapi', required: false },
    '-router': { des: '生成路由', required: false },
    '-store': { des: '生成vue store', required: false },
    '-view': { des: '生成vue视图', required: false },
    '-composable': { des: '生成composable', required: false }
  }
  // 分析命令  templat
  if (inputLine.indexOf('--h') > -1 || inputLine.indexOf('--help') > -1) {
    console.log('=======================================各参数说明如下=====================================')
    console.log('\n')
    // const tab = '\t'
    // console.log(`${tab}参数${tab}说明${tab}是否必填${tab}`)
    for (const key in explation) {
      const value = explation[key]
      console.log(`${key}:${value.des}  是否必填:${value.required}\n`)
    }
    console.log('\n')
    console.log('=======================================================================================')
    main()
  }
  const pattern = /(--h|-\w+)\s+(\w*)/g
  let result = null
  const params = []
  const paramsValue = []
  do {
    result = pattern.exec(inputLine)
    if (result && result.length >= 2) {
      params.push(result[1])
      paramsValue.push(result[2])
    }
  } while (result)
  if (params.length === 0) {
    main()
    return
  }
  // 验证参数有效性
  for (const index in params) {
    if (commands.indexOf(params[index]) === -1) {
      console.log(`未识别的命令:${params[index]},请核对后输入`)
      main()
      return
    }
  }
  // 读取命令
  for (const index in params) {
    const command = params[index]
    const tableIndex = params.findIndex((command) => command === '-t')
    let table = ''
    // const value = paramsValue[index]
    if (command === '-skeleton') {
      // 创建骨架图
      createSkeleton()
    } else {
      if (tableIndex === -1) {
        console.log('请输入-t参数值')
        main()
        return
      } else {
        table = paramsValue[tableIndex]
      }
      if (params.length === 1) {
        // 默认创建所有资源
        tableModel(table, '-all')
      } else {
        tableModel(table, command)
      }
    }
  }
}
main()
